package com.cskaoyan.controller.backstagecontroller;

import com.cskaoyan.bean.data.BaseData;
import com.cskaoyan.bean.param.AdParam;
import com.cskaoyan.bean.param.CouponDetail;
import com.cskaoyan.bean.param.CouponParam;
import com.cskaoyan.bean.param.TopicParam;
import com.cskaoyan.bean.pojo.*;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.service.AdService;
import com.cskaoyan.service.CouponService;
import com.cskaoyan.service.TopicService;
import com.cskaoyan.utils.backstage.ValidateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("admin")
public class PromotionController {
    @Autowired
    AdService adService;

    @Autowired
    CouponService couponService;

    @Autowired
    TopicService topicService;

    /* ---------------ad module--------------- */
    @RequestMapping("ad/list")
    public BaseRespVo adList(AdParam param) {
        BaseData<MallAd> mallAdBaseData = adService.queryList(param);
        return BaseRespVo.ok(mallAdBaseData);
    }

    @RequestMapping("ad/delete")
    public BaseRespVo deleteAd(@RequestBody MallAd mallAd) {
        int success = adService.deleteAd(mallAd);
        if (success == 200) {
            return BaseRespVo.ok();
        }
        return BaseRespVo.fail();
    }

    @RequestMapping("ad/update")
    public BaseRespVo updateAd(@RequestBody @Validated MallAd mallAd, BindingResult result) {
        if(result.hasErrors()){
           return ValidateUtils.validate(result);
        }
        int success = adService.updateAd(mallAd);
        if (success == 200) {
            return BaseRespVo.ok(mallAd);
        }
        return BaseRespVo.fail();
    }





    @RequestMapping("ad/create")
    public BaseRespVo createAd(@RequestBody @Validated  MallAd mallAd,BindingResult result) {
        if(result.hasErrors()){
            return ValidateUtils.validate(result);
        }
        MallAd mallAd1 = adService.createAd(mallAd);
        if (mallAd1 != null) {
            return BaseRespVo.ok(mallAd1);
        }
        return BaseRespVo.fail();
    }

    @RequestMapping("coupon/list")
    public BaseRespVo couponList(CouponParam couponParam) {
        BaseData<MallCoupon> mallCouponBaseData = couponService.queryList(couponParam);
        return BaseRespVo.ok(mallCouponBaseData);
    }

    @RequestMapping("coupon/listuser")
    public BaseRespVo couponListUser(CouponDetail couponDetail) {
        BaseData<MallCouponUser> items =couponService.queryUserList(couponDetail);
        return BaseRespVo.ok(items);
    }

    @RequestMapping("coupon/read")
    public BaseRespVo couponRead(Integer id) {
        MallCoupon mallCoupon =couponService.read(id);
        return BaseRespVo.ok(mallCoupon);
    }

    @RequestMapping("coupon/create")
    public BaseRespVo createCoupon(@RequestBody @Validated MallCoupon mallCoupon,BindingResult result) {
        if(result.hasFieldErrors()){
            return ValidateUtils.validate(result);
        }
        MallCoupon mallCoupon1 = couponService.create(mallCoupon);
        if (mallCoupon1 != null) {
            return BaseRespVo.ok(mallCoupon1);
        }
        return BaseRespVo.fail();
    }

    @RequestMapping("coupon/update")
    public BaseRespVo updateCoupon(@RequestBody @Validated MallCouponUpdate mallCouponUpdate,BindingResult result) {
        if(result.hasFieldErrors()){
            return ValidateUtils.validate(result);
        }
        MallCoupon mallCoupon = couponService.update(mallCouponUpdate);
        if (mallCoupon != null) {
            return BaseRespVo.ok(mallCoupon);
        }
        return BaseRespVo.fail();
    }

    @RequestMapping("coupon/delete")
    public BaseRespVo deleteCoupon(@RequestBody MallCoupon mallCoupon) {
        int success = couponService.delete(mallCoupon);
        if(success ==200){
            return BaseRespVo.ok();
        }
        return BaseRespVo.fail();
    }

    @RequestMapping("topic/list")
    public BaseRespVo topicList(TopicParam param) {
        BaseData<MallTopic> mallTopicBaseData = topicService.queryList(param);
        return BaseRespVo.ok(mallTopicBaseData);
    }


    @RequestMapping("topic/update")
    public BaseRespVo updateTopic(@RequestBody @Validated MallTopic mallTopic,BindingResult result) {
        if(result.hasFieldErrors()){
            return ValidateUtils.validate(result);
        }
        int success = topicService.updateTopic(mallTopic);
        if (success == 200) {
            return BaseRespVo.ok(mallTopic);
        }
        return BaseRespVo.fail();
    }

    @RequestMapping("topic/delete")
    public BaseRespVo deleteTopic(@RequestBody MallTopic mallTopic) {
        int success = topicService.deleteTopic(mallTopic);
        if (success == 200) {
            return BaseRespVo.ok();
        }
        return BaseRespVo.fail();
    }

    @RequestMapping("topic/create")
    public BaseRespVo createTopic(@RequestBody @Validated MallTopic mallTopic,BindingResult result){
        if(result.hasFieldErrors()){
            return ValidateUtils.validate(result);
        }
        MallTopic mallTopic1=topicService.createTopic(mallTopic);
        if(mallTopic1!=null){
            return BaseRespVo.ok(mallTopic1);
        }
        return BaseRespVo.fail();
    }


}
